﻿

var PlayLayer = cc.Layer.extend({
    bgSprite: null,
    SushiSprites: null,
    score: 0,

    ctor: function () {
        this._super();
        var size = cc.winSize;
        this.bgSprite = new cc.Sprite(res.BackGround_png);
        this.bgSprite.attr({
            x: size.width / 2,
            y: size.height / 2,
        });

        this.addChild(this.bgSprite);
        var size = cc.director.getWinSize();
        this.removeChild(this.scoreLabel);
        this.scoreLabel = new cc.LabelTTF("score:" + this.score, "Arial", 20);
        this.scoreLabel.attr({
            x: size.width / 2 + 100,
            y: size.height - 20
        });
        this.addChild(this.scoreLabel, 5);
        this.SushiSprites = [];
        this.schedule(this.update, 1, 16 * 1024, 1);
        return true;
    },

    update: function () {
        this.removeSushi();
        this.addSushi();
      
       
    },
    updateScore: function () {
        this.score++;
        cc.log("updateScore");
        this.scoreLabel.setString("score:" + this.score);
    },

    addSushi: function () {        
        var sushi = new SushiSprite("res/sushi_1n.png");
        var size = cc.winSize;
        var x = sushi.width / 2 + size.width / 2 * cc.random0To1();
        sushi.attr({
            x: x,
            y: size.height - 30
        });
        this.addChild(sushi, 5);
        var dorpAction = cc.MoveTo.create(4, cc.p(sushi.x, -30));
        sushi.runAction(dorpAction);
        sushi.index = this.SushiSprites.length;
        this.SushiSprites.push(sushi);

        
        
      

    },

    removeSushi: function () {      
        for (var i = 0; i < this.SushiSprites.length; i++) {           
            if (0 > this.SushiSprites[i].y) {
                cc.log("==============remove:" + i);
                this.SushiSprites[i].removeFromParent();
                this.SushiSprites[i] = undefined;
                this.SushiSprites.splice(i, 1);
                i = i - 1;
            }
        }
    },   
   
  
    
});

var PlayScene = cc.Scene.extend({
    onEnter: function () {
        this._super();
        var layer = new PlayLayer();
        this.addChild(layer);
    }
});
